package ua.org.nuos.sdms.clientgui.client.handlers;

import ua.org.nuos.sdms.clientgui.client.controller.GlobalBoard;
import ua.org.nuos.sdms.clientgui.client.events.ShowSelectGroupEvent;
import ua.org.nuos.sdms.clientgui.client.events.UserChangedEvent;
import ua.org.nuos.sdms.clientgui.client.listeners.UserChangedListener;
import ua.org.nuos.sdms.clientgui.server.AppProxy;
import ua.org.nuos.sdms.middle.entity.Group;
import ua.org.nuos.sdms.middle.entity.User;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 04.03.12
 * Time: 16:23
 * To change this template use File | Settings | File Templates.
 */
public class CheckUserLastGroupHandler implements UserChangedListener {

    @Override
    public void userChanged(UserChangedEvent event) {
        User user = AppProxy.getInstance().getServiceLocator().getAuthenticationBean().getCurrentUser();
        if (user == null) {
            return;
        }
        Group group = user.getLastGroup();
        if (group == null) {
            AppProxy.getInstance().getController(GlobalBoard.ID).fire(new ShowSelectGroupEvent());
        } else {
            AppProxy.getInstance().getModelLocator().setGroup(group);
        }
    }
}
